#!/usr/bin/env bash

# elasticSearch配置文件
function elasticSearch_config() {
  echo_sleep "\n开始写入配置文件……"
  echo "http.host: 0.0.0.0" >>${applicationPath}/config/elasticSearch.yml
  echo "http.port: 9200" >>${applicationPath}/config/elasticSearch.yml
  echo_sleep "配置文件写入完成！\n"
}

# 判断容器是否启动成功
function elasticSearch_successOrNot() {
  echo_sleep "docker run命令:\n $dockerRunCmd \n" 2
  if [[ -n $(docker ps -q -f "name=^$application$") ]]; then
    echo -e "$application 容器启动成功"
    echo -e "\t端口号：$port"
    echo -e "\t版本号：$version"
    echo -e "\t容器名称：$application"
    echo -e "\t本地映射路径：$applicationPath"
    echo -e "进入容器使用命令：docker exec -it $application /bin/bash"
    echo -e "web页面访问地址: http://host:${port}"
  else
    log_error "$application 容器启动失败,请检查docker run命令是否有误,端口是否被占用"
  fi
}

# 输入需要修改的信息
function elasticSearch_input() {
  sleep 1
  read -r -p "9200端口需要映射到的端口(默认为 ${esPort})：" inputPort
  read -r -p "9300端口需要映射到的端口(默认为 ${esPort1})：" inputPort1
  read -r -p "$application容器的名称(默认为 $application)：" inputName
  read -r -p "$application容器内存(默认为 ${javaOpts}M)：" inputJavaOpts
  echo -e "\n开始启动容器……\n"

  if [ "$inputPort" != "" ]; then
    esPort=$inputPort
  fi
  if [ "$inputPort1" != "" ]; then
    esPort1=$inputPort1
  fi
  if [ "$inputName" != "" ]; then
    application=$inputName
  fi
  if [ "$inputJavaOpts" != "" ]; then
    javaOpts=$inputJavaOpts
  fi
}

# 安装elasticSearch
function elasticSearch_install() {
  application="elasticsearch"                         # 应用名称,docker ps中显示的名称
  image="elasticsearch"                               # docker仓库中镜像名称
  applicationPath="/opt/elasticsearch/${application}" # 本地映射文件路径
  port="9200"                                         # 对外映射的端口号
  version="7.17.10"                                   # 镜像版本号,默认为7.17.10 8.0以上安装报错
  dockerRunCmd=""                                     # 最后执行的docker run命令

  javaOpts="512"
  esPort="9200"
  esPort1="9300"

  echo -e "\n=========================开始安装Elasticsearch========================="

  echo_yellow "1.检测是否存在 ${application} 容器"
  docker_delExisting ${application}

  echo_yellow "2.开始拉取镜像文件"
  echo_red "不要安装8.0以上的版本,启动不了"
  docker_pull

  echo_yellow "3.开始创建本地映射目录"
  create_file_path config data plugins

  echo_yellow "4.开始创建配置文件"
  elasticSearch_config

  echo_yellow "5.创建并启动docker容器"
  esPort=$(read_input "9200端口需要映射到的端口(默认为 ${esPort})：" ${esPort})
  esPort1=$(read_input "9300端口需要映射到的端口(默认为 ${esPort1})：" ${esPort1})
  application=$(read_input "请输入容器的名称(默认为 ${application})：" ${application})
  javaOpts=$(read_input "${application}容器内存(默认为 ${javaOpts}M)：" ${javaOpts})

  echo -e "${application}启动中,请等待......"

  dockerRunCmd="docker run --restart=always --privileged=true \
				 --name $application -p $esPort:9200 \
				 -p $esPort1:9300 \
				 -e \"discovery.type=single-node\" \
				 -e ES_JAVA_OPTS=\"-Xms${javaOpts}m -Xmx${javaOpts}m\" \
				 -v $applicationPath/config/elasticSearch.yml:/usr/share/elasticSearch/config/elasticSearch.yml \
				 -v $applicationPath/data:/usr/share/elasticSearch/data \
				 -v $applicationPath/plugins:/usr/share/elasticSearch/plugins \
	             -v /etc/localtime:/etc/localtime:ro \
				 -d $image:$version &> /dev/null"
  eval $dockerRunCmd

  echo_yellow "6.检测容器是否启动成功"
  elasticSearch_successOrNot

  echo -e "=========================安装Elasticsearch结束========================="
}
